
<!DOCTYPE html>
<html lang="en" class="loading">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
    <title>“前端特效-放大镜案例” - Bittersweet.的博客</title>
    <meta name="apple-mobile-web-app-capable" content="yes" />
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="google" content="notranslate" />
    <meta name="keywords" content="bittersweet,"> 
    <meta name="description" content="我在学校里做的一些项目，以及遇到的问题的解决方法，希望为有需要的人提供帮助,纯原生js实现实现放大镜特效**先上效果图


接下来是html概览，因为要通过js去操作dom做特效需要在html里拿到id或class 

js代码实现window.addEventListen,"> 
    <meta name="author" content="Bittersweet.(姚亮)"> 
    <link rel="alternative" href="atom.xml" title="Bittersweet.的博客" type="application/atom+xml"> 
    <link rel="icon" href="/Bittersweet/img/favicon.ico"> 
    
    <meta name="twitter:image:src" content="https://bangdream.moe/img/ident.png"/>
    <meta name="twitter:creator" content="@yourtwitteridhere"/>
    <meta name="twitter:card" content="summary_large_image"/>
    <meta name="twitter:title" content="“前端特效-放大镜案例” - Bittersweet.的博客"/>
    <meta name="twitter:description" content="我在学校里做的一些项目，以及遇到的问题的解决方法，希望为有需要的人提供帮助,纯原生js实现实现放大镜特效**先上效果图


接下来是html概览，因为要通过js去操作dom做特效需要在html里拿到id或class 

js代码实现window.addEventListen,"/>
    <meta property="og:image" content="https://bangdream.moe/img/ident.png"/>
    <meta property="og:image:alt" content="我在学校里做的一些项目，以及遇到的问题的解决方法，希望为有需要的人提供帮助,纯原生js实现实现放大镜特效**先上效果图


接下来是html概览，因为要通过js去操作dom做特效需要在html里拿到id或class 

js代码实现window.addEventListen,"/>
    <meta property="og:image:width" content="1200"/>
    <meta property="og:image:height" content="600"/>
    <meta property="og:site_name" content="Bittersweet.的博客"/>
    <meta property="og:type" content="object"/>
    <meta property="og:title" content="“前端特效-放大镜案例” - Bittersweet.的博客"/>
    <meta property="og:description" content="我在学校里做的一些项目，以及遇到的问题的解决方法，希望为有需要的人提供帮助,纯原生js实现实现放大镜特效**先上效果图


接下来是html概览，因为要通过js去操作dom做特效需要在html里拿到id或class 

js代码实现window.addEventListen,"/>
    
<link rel="stylesheet" href="/Bittersweet/css/diaspora.css">

    <script>window.searchDbPath = "/Bittersweet/search.xml";</script>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Source+Code+Pro&display=swap" rel="stylesheet">
<meta name="generator" content="Hexo 6.3.0"></head>

<body class="loading">
    <span id="config-title" style="display:none">Bittersweet.的博客</span>
    <div id="loader"></div>
    <div id="single">
    <div id="top" style="display: block;">
    <div class="bar" style="width: 0;"></div>
    <a class="iconfont icon-home image-icon" href="javascript:;" data-url="http://gitee.com/yao-liang-8848"></a>
    <div title="播放/暂停" class="iconfont icon-play"></div>
    <h3 class="subtitle">“前端特效-放大镜案例”</h3>
    <div class="social">
        <div>
            <div class="share">
                <a title="获取二维码" class="iconfont icon-scan" href="javascript:;"></a>
            </div>
            <div id="qr"></div>
        </div>
    </div>
    <div class="scrollbar"></div>
</div>

    <div class="section">
        <div class="article">
    <div class='main'>
        <h1 class="title">“前端特效-放大镜案例”</h1>
        <div class="stuff">
            <span>十月 30, 2022</span>
            
  <ul class="post-tags-list" itemprop="keywords"><li class="post-tags-list-item"><a class="post-tags-list-link" href="/Bittersweet/tags/html/" rel="tag">html</a></li><li class="post-tags-list-item"><a class="post-tags-list-link" href="/Bittersweet/tags/js%E7%89%B9%E6%95%88/" rel="tag">js特效</a></li><li class="post-tags-list-item"><a class="post-tags-list-link" href="/Bittersweet/tags/web%E5%89%8D%E7%AB%AF/" rel="tag">web前端</a></li><li class="post-tags-list-item"><a class="post-tags-list-link" href="/Bittersweet/tags/%E6%95%99%E5%AF%BC%E6%96%87%E7%AB%A0/" rel="tag">教导文章</a></li><li class="post-tags-list-item"><a class="post-tags-list-link" href="/Bittersweet/tags/%E8%90%8C%E6%96%B0/" rel="tag">萌新</a></li></ul>


        </div>
        <div class="content markdown">
            <h1 id="纯原生js实现实现放大镜特效"><a href="#纯原生js实现实现放大镜特效" class="headerlink" title="纯原生js实现实现放大镜特效"></a>纯原生js实现实现放大镜特效</h1><p>**先上效果图</p>
<p><img src="http://bittersweetyao.gitee.io/pic/pic/amiplifier1.png"></p>
<p><img src="http://bittersweetyao.gitee.io/pic/pic/amiplifier2.png"></p>
<p><code>接下来是html概览，因为要通过js去操作dom做特效需要在html里拿到id或class </code></p>
<p><img src="http://bittersweetyao.gitee.io/pic/pic/amiplifierCode.png"></p>
<h2 id="js代码实现"><a href="#js代码实现" class="headerlink" title="js代码实现"></a>js代码实现</h2><pre class="line-numbers language-javascript" data-language="javascript"><code class="language-javascript">window<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">'load'</span><span class="token punctuation">,</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>

  <span class="token keyword">var</span> box<span class="token operator">=</span>document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">'.img'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword">var</span> move<span class="token operator">=</span>document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">'.move'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token keyword">var</span> big<span class="token operator">=</span>document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">'.big'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

  <span class="token comment">//鼠标的进出去决定盒子的出现</span>

  box<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">'mouseover'</span><span class="token punctuation">,</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>

​    move<span class="token punctuation">.</span>style<span class="token punctuation">.</span>display<span class="token operator">=</span><span class="token string">'block'</span><span class="token punctuation">;</span>

​    big<span class="token punctuation">.</span>style<span class="token punctuation">.</span>display<span class="token operator">=</span><span class="token string">'block'</span><span class="token punctuation">;</span>

​    box<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">'mouseout'</span><span class="token punctuation">,</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>

​      move<span class="token punctuation">.</span>style<span class="token punctuation">.</span>display<span class="token operator">=</span><span class="token string">'none'</span><span class="token punctuation">;</span>

​      big<span class="token punctuation">.</span>style<span class="token punctuation">.</span>display<span class="token operator">=</span><span class="token string">'none'</span><span class="token punctuation">;</span>

​    <span class="token punctuation">&#125;</span><span class="token punctuation">)</span>

  <span class="token punctuation">&#125;</span><span class="token punctuation">)</span>

  <span class="token comment">//鼠标跟随</span>

   box<span class="token punctuation">.</span><span class="token function">addEventListener</span><span class="token punctuation">(</span><span class="token string">'mousemove'</span><span class="token punctuation">,</span><span class="token keyword">function</span><span class="token punctuation">(</span><span class="token parameter">e</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span><span class="token comment">//这里的e为event事件的缩写，用操作对象</span>
<span class="token comment">//的回调来做形参</span>
​    <span class="token keyword">var</span> x<span class="token operator">=</span>e<span class="token punctuation">.</span>pageX<span class="token operator">-</span><span class="token keyword">this</span><span class="token punctuation">.</span>offsetLeft<span class="token punctuation">;</span>

​    <span class="token keyword">var</span> y<span class="token operator">=</span>e<span class="token punctuation">.</span>pageY<span class="token operator">-</span><span class="token keyword">this</span><span class="token punctuation">.</span>offsetTop<span class="token punctuation">;</span>

​    moveX<span class="token operator">=</span>x<span class="token operator">-</span>move<span class="token punctuation">.</span>offsetWidth<span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">;</span>

​    moveY<span class="token operator">=</span>y<span class="token operator">-</span>move<span class="token punctuation">.</span>offsetHeight<span class="token operator">/</span><span class="token number">2</span><span class="token punctuation">;</span>

​    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>box<span class="token punctuation">.</span>offsetWidth<span class="token operator">-</span>move<span class="token punctuation">.</span>offsetWidth<span class="token punctuation">)</span><span class="token punctuation">;</span>

​    <span class="token comment">//让move只能在盒子范围内移动</span>

​    <span class="token keyword">if</span><span class="token punctuation">(</span>moveX<span class="token operator">&lt;=</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>

​      moveX<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>

​    <span class="token punctuation">&#125;</span><span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>moveX<span class="token operator">>=</span>box<span class="token punctuation">.</span>offsetWidth<span class="token operator">-</span>move<span class="token punctuation">.</span>offsetWidth<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>

​      moveX<span class="token operator">=</span>box<span class="token punctuation">.</span>offsetWidth<span class="token operator">-</span>move<span class="token punctuation">.</span>offsetWidth<span class="token punctuation">;</span>

​    <span class="token punctuation">&#125;</span>

​    <span class="token keyword">if</span><span class="token punctuation">(</span>moveY<span class="token operator">&lt;=</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">&#123;</span>

​      moveY<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>

​    <span class="token punctuation">&#125;</span><span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span>moveY<span class="token operator">>=</span>box<span class="token punctuation">.</span>offsetHeight<span class="token operator">-</span>move<span class="token punctuation">.</span>offsetHeight<span class="token punctuation">)</span><span class="token punctuation">&#123;</span>

​      moveY<span class="token operator">=</span>box<span class="token punctuation">.</span>offsetHeight<span class="token operator">-</span>move<span class="token punctuation">.</span>offsetHeight<span class="token punctuation">;</span>

​    <span class="token punctuation">&#125;</span>

​    move<span class="token punctuation">.</span>style<span class="token punctuation">.</span>left<span class="token operator">=</span>moveX<span class="token operator">+</span><span class="token string">'px'</span><span class="token punctuation">;</span>

​    move<span class="token punctuation">.</span>style<span class="token punctuation">.</span>top<span class="token operator">=</span>moveY<span class="token operator">+</span><span class="token string">'px'</span><span class="token punctuation">;</span>

​    movemax<span class="token operator">=</span>box<span class="token punctuation">.</span>offsetWidth<span class="token operator">-</span>move<span class="token punctuation">.</span>offsetWidth<span class="token punctuation">;</span>

​    <span class="token comment">//大图的移动,按比例移动</span>

​    <span class="token keyword">var</span> b<span class="token operator">=</span>document<span class="token punctuation">.</span><span class="token function">querySelector</span><span class="token punctuation">(</span><span class="token string">'.b'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

​    <span class="token keyword">var</span> bMax<span class="token operator">=</span>b<span class="token punctuation">.</span>offsetWidth<span class="token operator">-</span>big<span class="token punctuation">.</span>offsetWidth<span class="token punctuation">;</span>

​    console<span class="token punctuation">.</span><span class="token function">log</span><span class="token punctuation">(</span>bMax<span class="token punctuation">)</span><span class="token punctuation">;</span>

​    <span class="token keyword">var</span> bX<span class="token operator">=</span>moveX<span class="token operator">*</span>bMax<span class="token operator">/</span>movemax<span class="token punctuation">;</span>

​    <span class="token keyword">var</span> bY<span class="token operator">=</span>moveY<span class="token operator">*</span>bMax<span class="token operator">/</span>movemax<span class="token punctuation">;</span>
<span class="token comment">//大图的移动方向应该与鼠标相反，所以是-bx,-by</span>
​    b<span class="token punctuation">.</span>style<span class="token punctuation">.</span>marginLeft<span class="token operator">=</span><span class="token punctuation">(</span><span class="token operator">-</span>bX<span class="token operator">+</span><span class="token string">'px'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

​    b<span class="token punctuation">.</span>style<span class="token punctuation">.</span>marginTop<span class="token operator">=</span><span class="token punctuation">(</span><span class="token operator">-</span>bY<span class="token operator">+</span><span class="token string">'px'</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

   <span class="token punctuation">&#125;</span><span class="token punctuation">)</span>   



<span class="token punctuation">&#125;</span><span class="token punctuation">)</span><span aria-hidden="true" class="line-numbers-rows"><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span><span></span></span></code></pre>


            <!--[if lt IE 9]><script>document.createElement('audio');</script><![endif]-->
            <audio id="audio" loop="1" preload="auto" controls="controls" data-autoplay="false">
                <source type="audio/mpeg" src="">
            </audio>
            
                <ul id="audio-list" style="display:none">
                    
                        
                            <li title="0" data-url="http://link.hhtjim.com/163/425570952.mp3"></li>
                        
                    
                        
                            <li title="1" data-url="http://link.hhtjim.com/163/425570952.mp3"></li>
                        
                    
                </ul>
            
        </div>
        
        
    <div id="gitalk-container" class="comment link"
		data-enable="false"
        data-ae="false"
        data-ci=""
        data-cs=""
        data-r=""
        data-o=""
        data-a=""
        data-d="false"
    >查看评论</div>


    </div>
    
        <div class="side">
            <ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%BA%AF%E5%8E%9F%E7%94%9Fjs%E5%AE%9E%E7%8E%B0%E5%AE%9E%E7%8E%B0%E6%94%BE%E5%A4%A7%E9%95%9C%E7%89%B9%E6%95%88"><span class="toc-number">1.</span> <span class="toc-text">纯原生js实现实现放大镜特效</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#js%E4%BB%A3%E7%A0%81%E5%AE%9E%E7%8E%B0"><span class="toc-number">1.1.</span> <span class="toc-text">js代码实现</span></a></li></ol></li></ol>
        </div>
    
</div>


    </div>
</div>
<script src="/Bittersweet/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"pluginRootPath":"live2dw/","pluginJsPath":"lib/","pluginModelPath":"assets/","tagMode":false,"debug":false,"model":{"jsonPath":"/Bittersweet/live2dw/assets/assets/koharu.model.json"},"display":{"position":"right","width":200,"height":400},"mobile":{"show":true},"react":{"opacity":0.8},"log":false});</script></body>


<script src="//lib.baomitu.com/jquery/1.8.3/jquery.min.js"></script>
<script src="/Bittersweet/js/plugin.js"></script>
<script src="/Bittersweet/js/typed.js"></script>
<script src="/Bittersweet/js/diaspora.js"></script>


<link rel="stylesheet" href="/Bittersweet/photoswipe/photoswipe.css">
<link rel="stylesheet" href="/Bittersweet/photoswipe/default-skin/default-skin.css">


<script src="/Bittersweet/photoswipe/photoswipe.min.js"></script>
<script src="/Bittersweet/photoswipe/photoswipe-ui-default.min.js"></script>


<!-- Root element of PhotoSwipe. Must have class pswp. -->
<div class="pswp" tabindex="-1" role="dialog" aria-hidden="true">
    <!-- Background of PhotoSwipe. 
         It's a separate element as animating opacity is faster than rgba(). -->
    <div class="pswp__bg"></div>
    <!-- Slides wrapper with overflow:hidden. -->
    <div class="pswp__scroll-wrap">
        <!-- Container that holds slides. 
            PhotoSwipe keeps only 3 of them in the DOM to save memory.
            Don't modify these 3 pswp__item elements, data is added later on. -->
        <div class="pswp__container">
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
            <div class="pswp__item"></div>
        </div>
        <!-- Default (PhotoSwipeUI_Default) interface on top of sliding area. Can be changed. -->
        <div class="pswp__ui pswp__ui--hidden">
            <div class="pswp__top-bar">
                <!--  Controls are self-explanatory. Order can be changed. -->
                <div class="pswp__counter"></div>
                <button class="pswp__button pswp__button--close" title="Close (Esc)"></button>
                <button class="pswp__button pswp__button--share" title="Share"></button>
                <button class="pswp__button pswp__button--fs" title="Toggle fullscreen"></button>
                <button class="pswp__button pswp__button--zoom" title="Zoom in/out"></button>
                <!-- Preloader demo http://codepen.io/dimsemenov/pen/yyBWoR -->
                <!-- element will get class pswp__preloader--active when preloader is running -->
                <div class="pswp__preloader">
                    <div class="pswp__preloader__icn">
                      <div class="pswp__preloader__cut">
                        <div class="pswp__preloader__donut"></div>
                      </div>
                    </div>
                </div>
            </div>
            <div class="pswp__share-modal pswp__share-modal--hidden pswp__single-tap">
                <div class="pswp__share-tooltip"></div> 
            </div>
            <button class="pswp__button pswp__button--arrow--left" title="Previous (arrow left)">
            </button>
            <button class="pswp__button pswp__button--arrow--right" title="Next (arrow right)">
            </button>
            <div class="pswp__caption">
                <div class="pswp__caption__center"></div>
            </div>
        </div>
    </div>
</div>



<script type="text/x-mathjax-config">
    MathJax.Hub.Config({"HTML-CSS": { preferredFont: "TeX", availableFonts: ["STIX","TeX"], linebreaks: { automatic:true }, EqnChunk: (MathJax.Hub.Browser.isMobile ? 10 : 50) },
        tex2jax: { inlineMath: [ ["$", "$"], ["\\(","\\)"] ], processEscapes: true, ignoreClass: "tex2jax_ignore|dno",skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']},
        TeX: {  noUndefined: { attributes: { mathcolor: "red", mathbackground: "#FFEEEE", mathsize: "90%" } }, Macros: { href: "{}" } },
        messageStyle: "none"
    });
</script>
<script type="text/x-mathjax-config">
    MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for(i=0; i < all.length; i += 1) {
            all[i].SourceElement().parentNode.className += ' has-jax';
        }
    });
</script>

<script async type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML">
</script>




</html>
